unit basereports;

{$mode objfpc}{$H+}

interface

uses
  SysUtils, Classes, fpg_base, fpg_main, fpg_form, fpg_panel, fpg_label,
  fpg_basegrid, fpg_grid, fpg_scrollbar, fpg_button, fpg_popupcalendar,
  Sqlite3DS, fposUtils, fpstructs;

type

  { TShiftReport }

  TShiftReport = class(TfpgForm)
  public
    {@VFD_HEAD_BEGIN: ShiftReport}
    TopPanel: TfpgPanel;
    TitleLabel: TfpgLabel;
    StaffNameLabel: TfpgLabel;
    ShiftLabel: TfpgLabel;
    MainPanel: TfpgPanel;
    ReportGrid: TfpgStringGrid;
    BottomPanel: TfpgPanel;
    CloseButton: TfpgButton;
    FromToPanel: TfpgPanel;
    Label1: TfpgLabel;
    FromCalendarCombo: TfpgCalendarCombo;
    ToCalendarCombo: TfpgCalendarCombo;
    Label2: TfpgLabel;
    GoButton: TfpgButton;
    {@VFD_HEAD_END: ShiftReport}
    fDataDB: TSqlite3Dataset;
    procedure OnGoButtonClick(Sender: TObject);
    procedure CalendarChange(Sender: TObject);
    procedure CloseButtonClick(Sender: TObject);
  public
    FromTime: TJulianDateTime;
    ToTime: TJulianDateTime;
    constructor Create ( Data: TSqlite3Dataset ) ;
    procedure AfterCreate; override;
    procedure CreateReport;
  end;

{@VFD_NEWFORM_DECL}

var
  ShiftReport: TShiftReport;

implementation

uses fposclientdb;

{@VFD_NEWFORM_IMPL}

procedure TShiftReport.OnGoButtonClick ( Sender: TObject ) ;
var
  r: Integer;
  saleid: longint;
  total: extended;
begin
  fDataDB := ClientDB.SalesReport(FromTime, ToTime);
  r := 0;
  saleid := 0;
  total := 0;
  if fDataDB<>nil then
     begin
       ReportGrid.Clear;
       ReportGrid.ShowGrid := false;
       ReportGrid.Visible := false;
       ReportGrid.RowCount := fDataDB.RecordCount*2;
       ReportGrid.ColumnCount := 4;
       While not fDataDB.EOF do
             begin
               if fDataDB.FieldByName('saleid').AsInteger > saleid then
                  begin
                    if fDataDB.RecNo>1 then
                       begin
                         ReportGrid.Cells[2, r] := 'TOTAL';
                         ReportGrid.Cells[3, r] := FormatFloat(CurrencyString + ' 0.00', total);
                         Inc(r,2);
                       end;
                    saleid := fDataDB.FieldByName('saleid').AsInteger;
                    ReportGrid.Cells[0, r] := fDataDB.FieldByName('saleid').AsString;
                    ReportGrid.Cells[1, r] := FormatJulianDateTime('d mmm yyyy   hh:mm:ss', fDataDB.FieldByName('saletime').AsFloat);
                    Total := fDataDB.FieldByName('total').AsFloat;
                    ReportGrid.RowCount := ReportGrid.RowCount+2;
                    Inc(r);
                  end;
               ReportGrid.Cells[1, r] := fDataDB.FieldByName('itemname').AsString;
               ReportGrid.Cells[2, r] := 'x' + fDataDB.FieldByName('quantity').AsString;
               ReportGrid.Cells[3, r] := FormatFloat(CurrencyString + ' 0.00', fDataDB.FieldByName('price').AsFloat);
               Inc(r);
               fDataDB.Next;
             end;
             ReportGrid.Cells[2, r] := 'TOTAL';
             ReportGrid.Cells[3, r] := FormatFloat(CurrencyString + ' 0.00', total);
       ReportGrid.RowCount := r+2;
       ReportGrid.Visible := true;
       ReportGrid.ColumnWidth[1] := ReportGrid.Width - ReportGrid.ColumnWidth[0] - ReportGrid.ColumnWidth[2] - ReportGrid.ColumnWidth[3]-20;
     end;
end;

procedure TShiftReport.CalendarChange ( Sender: TObject ) ;
begin
  if Sender=FromCalendarCombo
     then FromTime := FromCalendarCombo.DateValue
  else ToTime := ToCalendarCombo.DateValue;
end;

procedure TShiftReport.CloseButtonClick(Sender: TObject);
begin
  Close;
  Self.Free;
end;

constructor TShiftReport.Create ( Data: TSqlite3Dataset ) ;
begin
  Inherited Create(nil);
  fDataDB := Data;
end;

procedure TShiftReport.AfterCreate;
begin
  {%region 'Auto-generated GUI code' -fold}
  {@VFD_BODY_BEGIN: ShiftReport}
  Name := 'ShiftReport';
  SetPosition(300, 150, 610, 635);
  WindowTitle := 'ShiftReport';
  Hint := '';

  TopPanel := TfpgPanel.Create(self);
  with TopPanel do
  begin
    Name := 'TopPanel';
    SetPosition(0, 0, 610, 116);
    Align := alTop;
    BackgroundColor := TfpgColor($FFFFFF);
    FontDesc := '#Label1';
    Hint := '';
    Text := '';
  end;

  TitleLabel := TfpgLabel.Create(TopPanel);
  with TitleLabel do
  begin
    Name := 'TitleLabel';
    SetPosition(116, 12, 332, 13);
    Alignment := taCenter;
    BackgroundColor := TfpgColor($FFFFFF);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Label';
  end;

  StaffNameLabel := TfpgLabel.Create(TopPanel);
  with StaffNameLabel do
  begin
    Name := 'StaffNameLabel';
    SetPosition(24, 40, 80, 13);
    BackgroundColor := TfpgColor($FFFFFF);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Label';
  end;

  ShiftLabel := TfpgLabel.Create(TopPanel);
  with ShiftLabel do
  begin
    Name := 'ShiftLabel';
    SetPosition(24, 64, 80, 13);
    BackgroundColor := TfpgColor($FFFFFF);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Label';
  end;

  MainPanel := TfpgPanel.Create(self);
  with MainPanel do
  begin
    Name := 'MainPanel';
    SetPosition(0, 116, 610, 519);
    Anchors := [anLeft,anRight,anTop,anBottom];
    Align := alClient;
    BackgroundColor := TfpgColor($FFFFFF);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Panel';
  end;

  ReportGrid := TfpgStringGrid.Create(MainPanel);
  with ReportGrid do
  begin
    Name := 'ReportGrid';
    SetPosition(12, 12, 582, 442);
    Anchors := [anLeft,anRight,anTop,anBottom];
    BackgroundColor := TfpgColor($80000002);
    FontDesc := '#Grid';
    HeaderFontDesc := '#GridHeader';
    Hint := '';
    RowCount := 0;
    RowSelect := False;
    TabOrder := 5;
  end;

  BottomPanel := TfpgPanel.Create(self);
  with BottomPanel do
  begin
    Name := 'BottomPanel';
    SetPosition(0, 587, 610, 48);
    Align := alBottom;
    BorderStyle := bsDouble;
    FontDesc := '#Label1';
    Hint := '';
    Style := bsLowered;
    Text := '';
  end;

  CloseButton := TfpgButton.Create(BottomPanel);
  with CloseButton do
  begin
    Name := 'CloseButton';
    SetPosition(524, 6, 80, 36);
    Anchors := [anRight,anTop];
    Text := 'Close';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    ModalResult := mrOK;
    TabOrder := 1;
    OnClick := @CloseButtonClick;
  end;

  FromToPanel := TfpgPanel.Create(TopPanel);
  with FromToPanel do
  begin
    Name := 'FromToPanel';
    SetPosition(2, 74, 606, 40);
    Align := alBottom;
    FontDesc := '#Label1';
    Hint := '';
    Text := '';
  end;

  Label1 := TfpgLabel.Create(FromToPanel);
  with Label1 do
  begin
    Name := 'Label1';
    SetPosition(20, 8, 80, 16);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'From';
  end;

  FromCalendarCombo := TfpgCalendarCombo.Create(FromToPanel);
  with FromCalendarCombo do
  begin
    Name := 'FromCalendarCombo';
    SetPosition(72, 8, 120, 24);
    BackgroundColor := TfpgColor($80000002);
    DateFormat := 'd/m/yyyy';
    DayColor := TfpgColor($000000);
    FontDesc := '#List';
    Hint := '';
    HolidayColor := TfpgColor($000000);
    SelectedColor := TfpgColor($000000);
    TabOrder := 2;
    OnChange := @CalendarChange;
  end;

  ToCalendarCombo := TfpgCalendarCombo.Create(FromToPanel);
  with ToCalendarCombo do
  begin
    Name := 'ToCalendarCombo';
    SetPosition(308, 8, 120, 24);
    BackgroundColor := TfpgColor($80000002);
    DateFormat := 'd/m/yyyy';
    DayColor := TfpgColor($000000);
    FontDesc := '#List';
    Hint := '';
    HolidayColor := TfpgColor($000000);
    SelectedColor := TfpgColor($000000);
    TabOrder := 3;
    OnChange := @CalendarChange;
  end;

  Label2 := TfpgLabel.Create(FromToPanel);
  with Label2 do
  begin
    Name := 'Label2';
    SetPosition(276, 12, 28, 16);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'to';
  end;

  GoButton := TfpgButton.Create(FromToPanel);
  with GoButton do
  begin
    Name := 'GoButton';
    SetPosition(456, 8, 56, 24);
    Text := 'Go';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 5;
    OnClick := @OnGoButtonClick;
  end;

  {@VFD_BODY_END: ShiftReport}
  {%endregion}
end;

procedure TShiftReport.CreateReport;
var
  saletime: Double;
  saletotal: Double;
  Gridrow: Integer;
begin
  ReportGrid.RowCount := fDataDB.RecordCount;
  Gridrow := 0;
  with ReportGrid do
       begin
        AddColumn('Sale #', 80, taLeftJustify);
        AddColumn('Time', 50, taLeftJustify);
        AddColumn('Item', 150, taLeftJustify);
        AddColumn('Qty', 50, taRightJustify);
        AddColumn('Subtotal',80, taRightJustify);
        AddColumn('Total', 80, taRightJustify);
        ColumnWidth[2] := Width-ColumnWidth[0]-ColumnWidth[1]-ColumnWidth[3]-ColumnWidth[4]-ColumnWidth[5]-20;
        ShowGrid := false;
        Options := Options + [go_HideFocusRect];
        ScrollBarStyle := ssVertical;
       end;
  saletime := fDataDB.FieldByName('saletime').AsFloat;
  saletotal:= fDataDB.FieldByName('total').AsFloat;
  ReportGrid.Cells[0, Gridrow] := fDataDB.FieldByName('saleid').AsString;
  ReportGrid.Cells[1, Gridrow] := FormatJulianDateTime('hh:mm', saletime);
  while not fDataDB.EOF do
        begin
          while saletime = fDataDB.FieldByName('saletime').AsFloat do
                begin
                  ReportGrid.Cells[2, Gridrow] := fDataDB.FieldByName('itemname').AsString;
                  ReportGrid.Cells[3, Gridrow] := fDataDB.FieldByName('quantity').AsString;
                  ReportGrid.Cells[4, Gridrow] := FormatFloat('0.00', fDataDB.FieldByName('price').AsFloat);
                  fDataDB.Next;
                  Inc(Gridrow);
                  if fDataDB.EOF then
                     begin
                       ReportGrid.Cells[5, Gridrow-1] := FormatFloat('0.00', saletotal);
                       Exit;
                     end;
                end;
          Inc(Gridrow);
          ReportGrid.RowCount := ReportGrid.RowCount+1;
          ReportGrid.Cells[5, Gridrow-2] := FormatFloat('0.00', saletotal);
          saletotal:= fDataDB.FieldByName('total').AsFloat;
          saletime := fDataDB.FieldByName('saletime').AsFloat;
          ReportGrid.Cells[0, Gridrow] := fDataDB.FieldByName('saleid').AsString;
          ReportGrid.Cells[1, Gridrow] := FormatJulianDateTime('hh:mm', saletime);
        end;
end;


end.
